package com.net.xpay.core.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.User;
import com.net.common.exception.BusinessException;
import com.net.common.util.AlipayAccountCheckUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.CommonAutoPayModel;
import com.net.xpay.core.utils.MoneyCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AutoPayValidator {

    public void validateAutoPay(User user, CommonAutoPayModel model) {
        log.info("validateAutoPay model={}|userId={}", SerializationUtil.obj2String(model), user.getId());
        if (Strings.isNullOrEmpty(model.getUid())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "付款链接错误");
        }

        if (!MoneyCheckUtil.isValidMoney(model.getTransAmount(), 2)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "转账金额格式错误，最多2位小数");
        }

        if (!AlipayAccountCheckUtil.isEffectiveAccount(model.getAlipayNickName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号格式不正确");
        }

        if (!Strings.isNullOrEmpty(model.getRemark()) && model.getRemark().length() > 50) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注字数不可以超过50字");
        }
    }
}
